<?php

namespace app\admin\controller;
use app\common\model\AdminUser as AdminUserModel;
use app\common\controller\AdminBase;
use think\Config;
use think\Db;
use think\Session;
/**
 * 个人设置
 * Class AdminUser
 * @package app\admin\controller
 */
class Personal extends AdminBase
{
    protected $admin_user_model;

    protected function _initialize()
    {
        parent::_initialize();
        $this->admin_user_model        = new AdminUserModel();
    }

    /**
     * 基本资料
     * @param string $keyword
     * @param int    $page
     * @return mixed
     */
    public function index()
    {
        $admin_user = $this->admin_user_model->find(Session::get('admin_id'));
        return $this->fetch('index', ['admin_user' => $admin_user]);
    }

    /**
     * 编辑基本资料
     * @param $id
     * @return mixed
     */
    public function edit()
    {
        $admin_user  = $this->admin_user_model->find(Session::get('admin_id'));
        return $this->fetch('edit', ['admin_user' => $admin_user]);
    }

    /**
     * 更新基本资料
     * @param $id
     */
    public function update()
    {
        if ($this->request->isPost()) {
            $data            = $this->request->post();
            $validate_result = $this->validate($data, 'AdminUser');

            if ($validate_result !== true) {
                $this->error($validate_result);
            } else {
                $admin_user              = $this->admin_user_model->find(Session::get('admin_id'));
                $admin_user->id          = Session::get('admin_id');
                $admin_user->img         = $data['img'];
                $admin_user->username   = $data['username'];
                $admin_user->name        = $data['name'];
                $admin_user->mobile      = $data['mobile'];
                $admin_user->email       = $data['email'];
                $admin_user->address     = $data['address'];
                if ($admin_user->save() !== false) {
                    $this->success('更新成功');
                } else {
                    $this->error('更新失败');
                }
            }
        }
    }

    /**
     * 修改密码
     * @param $id
     * @return mixed
     */
    public function passwordedit()
    {
        return $this->fetch();
    }

    /**
     * 更新密码
     * @param $id
     */
    public function passwordupdate()
    {
        if ($this->request->isPost()) {
            $data            = $this->request->post();
            $validate_result = $this->validate($data, 'Password');

            if ($validate_result !== true) {
                $this->error($validate_result);
            } else {
                $admin_user              = $this->admin_user_model->find(Session::get('admin_id'));
                $admin_user->id          = Session::get('admin_id');
                $admin_user->password = md5($data['password'] . Config::get('salt'));

                if ($admin_user->save() !== false) {
                    $this->success('更新成功');
                } else {
                    $this->error('更新失败');
                }
            }
        }
    }
    
}
